package catchData;//Catch4hljypcg

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;

import util.DateGroup;

/**
 * @author suwy
 * 2013-1-4
 * TODO GET THE IMAGE DATA
 */
public class Catch4hljypcg {
		private static Logger log = Logger.getLogger("Catch4hljypcg");
		private static HttpClient client;
		private static final String LOGIN_PAGE = "http://www.hljypcg.gov.cn/KB/Final_Gongshi.aspx";
		private static final String HOST = "www.hljypcg.gov.cn";
		private static final int pageNum = 1;
//		private static final String folderName = "2011年基本药物增补品种招标";

		private void initHttpClient() {
			if (client == null) {
				MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
				client = new HttpClient(connectionManager);
				// client.getHostConfiguration().setHost(HOST, PORT, "http"); //
				// 配置服务器参量
				client.getParams().setCookiePolicy(
						CookiePolicy.BROWSER_COMPATIBILITY); // 配置Cookie策略为浏览器默认策略
				client.getParams().setParameter(
						"http.protocol.single-cookie-header", true);
				client.getParams().setParameter(
						HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
				client.getHttpConnectionManager().getParams().setConnectionTimeout(
						300000);
				client.getHttpConnectionManager().getParams().setSoTimeout(300000);
			}
		}

		private void setHeaders(HttpMethod method) {
			method.getParams().setParameter("http.protocol.cookie-policy",
					CookiePolicy.BROWSER_COMPATIBILITY);
			method.setRequestHeader("Accept","*/*");
			method.setRequestHeader("Accept-Charse","GBK,utf-8;q=0.7,*;q=0.3");
			method.setRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
			method.setRequestHeader("Accept-Language", "zh-CN,zh;q=0.8");
			method.setRequestHeader("Connection", "keep-alive");
			method.setRequestHeader("host", HOST);
			method.setRequestHeader("Referer",LOGIN_PAGE);
			method.setRequestHeader("User-Agent",
							"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.91 Safari/537.11");
			
			method.setRequestHeader("cache-control", "no-cache");
		}

		private void saveDataFile(String sFolder, String data, long lid) {
			saveDataFile(sFolder, data, lid + "");
		}

		private void saveDataFile(String sFolder, String data, String sid) {
			File folder = new File(sFolder);
			if (!folder.exists()) folder.mkdirs();
			PrintWriter out = null;
			String saveFile = sFolder + "/" + sid + ".htm";
			try {
				out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(
						saveFile), "gbk"));
				out.println(data);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (out != null) {
						out.close();
						out = null;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				log.info("【" + DateGroup.getNow() + "】 保存文件成功！"
						+ saveFile.replaceAll("\\\\", "/"));
			}
		}


		public static void main(String[] args) {
			String mainURL = "http://www.hljypcg.gov.cn/KB/GsPic.aspx?keyword=&pageindex=0&pagesize=20&slid=201102&jb=0";
//			String filePath = "E:/downloadData/test/"+folderName;
			String str = null;
			PostMethod post = null;
			try {
				Catch4hljypcg instance = new Catch4hljypcg();
				instance.initHttpClient();
				for (int i = 1; i <= pageNum; i++) {
					post = new PostMethod(mainURL);
					instance.setHeaders(post);
					NameValuePair keyword = new NameValuePair("keyword","");
					NameValuePair pageindex = new NameValuePair("pageindex", ""+i);
					NameValuePair pagesize = new NameValuePair("pagesize", "20");
					NameValuePair slid = new NameValuePair("slid","201102");
					NameValuePair jb = new NameValuePair("jb","0");
					//NameValuePair __VIEWSTATE = new NameValuePair("__VIEWSTATE","5FX8rkWFtdN1LMyR/pmbz/7lL9Dz0d1O916dPeUq1UOtKVuF8O7BVKdf0d97Q3I/4IKb5wMkX3O7Eg3Bv4j8ZmDXzbzXelDMZpTyWv+EcgUgBmD9niyQT4TrM901owS4dFXP98WHrTVW9S1yAnYd+qGDupP/GrdaXDLxotKkHRsi2utHZA+Hb88eSpBQ82ZVqBiP+vnWhHUnQ7OyHfSGJjewWnGA1aGEO48EvGb8djDMHeOkLGoH8AZxgJTX6Pgpk5aEUQ==");
					
					post.setRequestBody(new NameValuePair[] { 
							keyword, pageindex, pagesize, slid, jb//,__VIEWSTATE
							});
					int stateCode = client.executeMethod(post);
					post.getParams().setContentCharset("GBK");
					str = post.getResponseBodyAsString();
//					System.out.println("code is:"+stateCode);
					System.out.println(str);
					post.releaseConnection();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
}